<?php

namespace app\models\Child;

use Yii;

/**
 * This is the model class for table "t_card_usr_rel".
 *
 * @property integer $rel_id
 * @property integer $user_id
 * @property integer $card_id
 * @property string $card_createtime
 * @property integer $card_left_time
 */
class TCardUsrRel extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 't_card_usr_rel';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'card_id', 'card_left_time'], 'integer'],
            [['card_createtime'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'rel_id' => 'Rel ID',
            'user_id' => 'User ID',
            'card_id' => 'Card ID',
            'card_createtime' => 'Card Createtime',
            'card_left_time' => 'Card Left Time',
        ];
    }
    
    /*
     * 获取用户次卡总次数
     */
    public function getCardCount($user_id)
    {
        $cards = $this->find()
                ->where(['user_id' => $user_id])
                ->count();
        return $cards;
    }
    
    /*
     * 获取用户次卡列表
     */
    public function getList($user_id)
    {
        
        $list = $this->find()
                ->select('card_name,card_left_time,card_count')
                ->join('left join','sys_account_card_info','sys_account_card_info.card_id = t_card_usr_rel.card_id')
                ->where(['user_id' => $user_id])
                ->asArray()
                ->all();
        return $list;
    }
}
